package com.ldq.imooc.basics.dataOperation;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author : 李道泉
 * @Description :
 * @Date : 2018/5/5 14:48
 * @Modified by :
 */
public class ProcedureDao {

    public static void proc_selectAllNoParams() throws SQLException {

        Connection conn = DBUtil.getConn();
        CallableStatement cs = conn.prepareCall("call proc_selectAllNoParams()");
        cs.execute();
        ResultSet rs = cs.getResultSet();

        while (rs.next()) {
            System.out.println(rs.getInt("id") + " " + rs.getString("user_name") + " " + rs.getString("email") + " " + rs.getString("mobile"));
        }
    }

    public static List<Goddess> proc_getInfoByParams(String param_name) throws Exception {

        Connection conn = DBUtil.getConn();
        CallableStatement cs = conn.prepareCall("call proc_getInfoByParams(?)");
        cs.setString(1, param_name);
        cs.execute();
        ResultSet resultSet = cs.getResultSet();

        List<Goddess> goddessList = new ArrayList<Goddess>();
        Goddess goddess = null;
        while (resultSet.next()) {

            goddess = new Goddess();
            goddess.setId(resultSet.getInt("id"));
            goddess.setUser_name(resultSet.getString("user_name"));
            goddess.setAge(resultSet.getInt("age"));
            goddessList.add(goddess);
        }
        return goddessList;
    }

    public static Integer proc_getCount() throws Exception {

        Integer count = 0;
        Connection conn = DBUtil.getConn();
        CallableStatement cs = conn.prepareCall("call proc_getCount(?)");
        cs.registerOutParameter(1, Types.INTEGER);
        cs.execute();
        count = cs.getInt(1);
        return count;
    }
}
